<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <span>筛选搜索</span>
      </div>

      <div style="margin-top: 15px">
        <el-form
          :inline="true"
          :model="searchsysUserList"
          size="small"
          label-width="140px"
        >
          <el-form-item label="姓名:">
            <el-input
              style="width: 203px"
              v-model="searchsysUserList.name"
              placeholder="请输入姓名"
              clearable
            ></el-input>
          </el-form-item>

          <el-form-item label="账号状态:">
            <el-select
              v-model="searchsysUserList.status"
              style="width: 203px"
              placeholder="请选择账号状态"
              clearable
            >
              <el-option
                v-for="item in statusOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>

      <div>
        <el-button
          @click="addsysUserDialogVisible = true"
          type="success"
          style="float: left"
          size="small"
          icon="el-icon-plus"
          >添加</el-button
        >
        <el-button
          @click="handleResetSearch()"
          type="info"
          style="float: right"
          size="small"
          icon="el-icon-delete"
          >重置</el-button
        >
        <el-button
          @click="getList(1)"
          style="float: right; margin-right: 15px; margin-bottom: 15px"
          type="primary"
          size="small"
          icon="el-icon-search"
          >查询</el-button
        >

        <el-dialog
          title="添加系统用户"
          :visible.sync="addsysUserDialogVisible"
          width="60%"
          :before-close="handleClose"
        >
          <span>
            <el-card class="filter-container" shadow="never">
              <div style="margin-top: 15px">
                <el-form
                  ref="addsysUserMessage"
                  :inline="true"
                  :model="addsysUserMessage"
                  size="small"
                  label-width="140px"
                >
                  <el-form-item label="姓名:">
                    <el-input
                      style="width: 203px"
                      v-model="addsysUserMessage.name"
                      placeholder="请输入员工姓名"
                      clearable
                    ></el-input>
                  </el-form-item>
                  <el-form-item label="性别:">
                    <el-select
                      v-model="addsysUserMessage.sex"
                      style="width: 203px"
                      placeholder="请选择性别"
                      clearable
                    >
                      <el-option
                        v-for="item in sexOptions"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                      >
                      </el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item label="生日:">
                    <el-date-picker
                      style="width: 203px"
                      v-model="addsysUserMessage.birth"
                      type="date"
                      placeholder="请选择日期"
                      clearable
                      value-format="yyyy-MM-dd"
                    >
                    </el-date-picker>
                  </el-form-item>
                  <el-form-item label="密码:">
                    <el-input
                      style="width: 203px"
                      v-model="addsysUserMessage.password"
                      placeholder="请输入密码"
                      clearable
                      show-password
                    ></el-input>
                  </el-form-item>
                  <el-form-item label="邮箱:">
                    <el-input
                      style="width: 203px"
                      v-model="addsysUserMessage.email"
                      placeholder="请输入邮箱"
                      clearable
                    ></el-input>
                  </el-form-item>
                  <el-form-item label="电话:">
                    <el-input
                      style="width: 203px"
                      v-model="addsysUserMessage.phone"
                      placeholder="请输入电话"
                      clearable
                    ></el-input>
                  </el-form-item>
                  <el-form-item label="地址:">
                    <el-input
                      style="width: 203px"
                      v-model="addsysUserMessage.address"
                      placeholder="请输入地址"
                      clearable
                    ></el-input>
                  </el-form-item>
                  <el-form-item label="照片:">
                    <el-upload
                      class="avatar-uploader"
                      action="http://47.120.7.70:8060/api/sys/uploadFace"
                      :show-file-list="false"
                      :on-success="handleAvatarSuccess"
                      :before-upload="beforeAvatarUpload"
                    >
                      <img
                        v-if="addsysUserMessage.faceUrl"
                        :src="addsysUserMessage.faceUrl"
                        class="avatar"
                      />
                      <i
                        v-else
                        class="el-icon-plus avatar-uploader-icon"
                        style="line-height: 178px; text-align: center"
                      ></i>
                    </el-upload>
                    <!-- <el-button
                      type="primary"
                      @click="uploadface;"
                      style="margin-bottom: 15px; width: 100%"
                      >上传人脸照片</el-button
                    > -->
                  </el-form-item>
                </el-form>
              </div>
            </el-card>
          </span>
          <span slot="footer" class="dialog-footer">
            <el-button @click="addsysUserDialogVisible = false"
              >取 消</el-button
            >
            <el-button type="primary" @click="addsysUser()">确 定</el-button>
          </span>
        </el-dialog>
      </div>
    </el-card>

    <div style="margin-top: 15px" class="table-container">
      <el-table
        :data="sysUserList"
        v-loading="listLoading"
        style="width: 100%"
        height="340px"
        border
      >
        <el-table-column
          type="selection"
          width="60"
          align="center"
        ></el-table-column>
        <el-table-column label="系统用户ID" width="150" align="center">
          <template slot-scope="scope">{{ scope.row.sysUserId }}</template>
        </el-table-column>
        <el-table-column label="姓名" width="150" align="center">
          <template slot-scope="scope">{{ scope.row.name }}</template>
        </el-table-column>
        <el-table-column label="性别" width="150" align="center">
          <template slot-scope="scope">
            <p v-if="scope.row.sex == 'SECRET'">保密</p>
            <p v-else-if="scope.row.sex == 'MAN'">男</p>
            <p v-else-if="scope.row.sex == 'WOMAN'">女</p>
            <p v-else>其他</p>
          </template>
        </el-table-column>
        <el-table-column label="E-mail" width="150" align="center">
          <template slot-scope="scope">{{ scope.row.email }}</template>
        </el-table-column>
        <el-table-column label="联系方式" width="150" align="center">
          <template slot-scope="scope">{{ scope.row.phone }}</template>
        </el-table-column>
        <el-table-column label="出生日期" width="150" align="center">
          <template slot-scope="scope">{{ scope.row.birth }}</template>
        </el-table-column>
        <el-table-column label="家庭住址" width="150" align="center">
          <template slot-scope="scope">{{ scope.row.address }}</template>
        </el-table-column>
        <el-table-column label="账号状态" width="134" align="center">
          <template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.status == 'BAN'">禁用</el-tag>
            <el-tag type="success" v-else-if="scope.row.status == 'NORMAL'"
              >正常</el-tag
            >
            <el-tag type="info" v-else>其他</el-tag></template
          >
        </el-table-column>
        <el-table-column
          label="创建时间"
          width="150"
          align="center"
          :formatter="formatDateC"
        >
          <template slot-scope="scope">{{ scope.row.createTime }}</template>
        </el-table-column>
        <el-table-column label="更新时间" width="150" align="center">
          <template slot-scope="scope">{{ scope.row.updateTime }}</template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="208" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="success"
              @click="
                editsysUserDialogVisible = true;
                editsysUserMessage = scope.row;
              "
              >修改</el-button
            >
            <el-button
              v-if="scope.row.status == 'NORMAL'"
              @click="handleBan(scope.row.sysUserId)"
              size="mini"
              type="warning"
              >禁用</el-button
            >
            <el-button
              v-if="scope.row.status == 'BAN'"
              @click="handleCancleBan(scope.row.sysUserId)"
              size="mini"
              type="primary"
              >解禁</el-button
            >
            <el-button
              @click="handleDelete(scope.row.sysUserId)"
              size="mini"
              type="danger"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-dialog
        title="系统用户基本信息修改"
        :visible.sync="editsysUserDialogVisible"
        width="60%"
        :before-close="handleClose"
      >
        <span>
          <el-card class="filter-container" shadow="never">
            <div style="margin-top: 15px">
              <el-form
                ref="editsysUserMessage"
                :inline="true"
                :model="editsysUserMessage"
                size="small"
                label-width="140px"
              >
                <el-form-item label="姓名:">
                  <el-input
                    style="width: 203px"
                    v-model="editsysUserMessage.name"
                    placeholder="请输入员工姓名"
                    clearable
                  ></el-input>
                </el-form-item>

                <el-form-item label="邮箱:">
                  <el-input
                    style="width: 203px"
                    v-model="editsysUserMessage.email"
                    placeholder="请输入员工邮箱"
                    clearable
                  ></el-input>
                </el-form-item>

                <el-form-item label="手机号:">
                  <el-input
                    style="width: 203px"
                    v-model="editsysUserMessage.phone"
                    placeholder="请输入员工手机号"
                    clearable
                  ></el-input>
                </el-form-item>

                <el-form-item label="家庭地址:">
                  <el-input
                    style="width: 203px"
                    v-model="editsysUserMessage.address"
                    placeholder="请输入家庭地址"
                    clearable
                  ></el-input>
                </el-form-item>
              </el-form>
            </div>
          </el-card>
        </span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editsysUserDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editsysUser()">确 定</el-button>
        </span>
      </el-dialog>
    </div>
    <div class="block">
      <el-pagination
        layout="prev, pager, next"
        :page-count="pageCunt"
        @current-change="getList"
        :current-page="pageNum"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
export default {
  name: "sysUserManage",
  data() {
    return {
      searchsysUserList: {
        name: "",
        status: "",
        pageNum: "1",
        pageSize: "5",
      },
      currentsysUserList: {
        name: "",
        email: "",
        phone: "",
        sex: "",
        status: "",
        pageNum: "1",
        pageSize: "5",
      },
      pageNum: 1,
      sysUserList: [],
      statusOptions: [
        {
          value: "BAN",
          label: "禁用",
        },
        {
          value: "NORMAL",
          label: "正常",
        },
      ],
      sexOptions: [
        {
          value: "WOMAN",
          label: "女性",
        },
        {
          value: "MAN",
          label: "男性",
        },
        {
          value: "SECRET",
          label: "保密",
        },
      ],
      addsysUserMessage: {
        address: "",
        birth: "",
        email: "",
        faceUrl: "",
        name: "",
        password: "",
        phone: "",
        sex: "",
      },
      editsysUserMessage: {
        address: "",
        birth: "",
        email: "",
        faceUrl: "",
        name: "",
        password: "",
        phone: "",
        sex: "",
      },
      ProvinceList: [],
      Addressoptions: {},
      pageCunt: 1,
      currentPage: 1,
      listLoading: true,
      addsysUserDialogVisible: false,
      editsysUserDialogVisible: false,
    };
  },
  created() {
    this.getList(1);
    // this.getProvinceList();
  },
  methods: {
    getList(pageNum) {
      this.currentsysUserList = this.searchsysUserList
      this.currentsysUserList.pageNum = pageNum;
      this.pageNum = pageNum;
      this.listLoading = true;
      this.axios
        .get("api/sys/auth/sysuser/list", {
          params: this.currentsysUserList,
        })
        .then((response) => {
          if (response.data.code == "200") {
            this.sysUserList = response.data.value.data;
            if (
              response.data.value.total / 5 >
              Number.parseInt(response.data.value.total / 5)
            ) {
              this.pageCunt =
                Number.parseInt(response.data.value.total / 5) + 1;
            } else {
              this.pageCunt = Number.parseInt(response.data.value.total / 5);
            }
            this.currentPage = 1;
            if (pageNum == 1) {
              if (response.data.value.total > 5) var total = 5;
              else var total = response.data.value.total;
            } else {
              var temp = response.data.value.total - (pageNum - 1) * 5;
              if (temp > 5) var total = 5;
              else var total = temp;
            }
            for (var i = 0; i < total; i++) {
            this.sysUserList[i].birth =
              new Date(response.data.value.data[i].birth).getFullYear() +
              "-" +
              (new Date(response.data.value.data[i].birth).getMonth() +
                1) +
              "-" +
              new Date(response.data.value.data[i].birth).getDate()

              this.sysUserList[i].createTime =
              new Date(response.data.value.data[i].createTime).getFullYear() +
              "-" +
              (new Date(response.data.value.data[i].createTime).getMonth() +
                1) +
              "-" +
              new Date(response.data.value.data[i].createTime).getDate() +
              " " +
              new Date(response.data.value.data[i].createTime).getHours() +
              ":" +
              new Date(response.data.value.data[i].createTime).getMinutes() +
              ":" +
              new Date(response.data.value.data[i].createTime).getSeconds();

              this.sysUserList[i].updateTime =
              new Date(response.data.value.data[i].updateTime).getFullYear() +
              "-" +
              (new Date(response.data.value.data[i].updateTime).getMonth() +
                1) +
              "-" +
              new Date(response.data.value.data[i].updateTime).getDate() +
              " " +
              new Date(response.data.value.data[i].updateTime).getHours() +
              ":" +
              new Date(response.data.value.data[i].updateTime).getMinutes() +
              ":" +
              new Date(response.data.value.data[i].updateTime).getSeconds();
          }
          } else {
            this.$message.error(response.data.message);
          }
          this.listLoading = false;
        })
        .catch((error) => {
          this.$message.error("无法获取数据");
          this.listLoading = false;
        });
    },
    editsysUser(){
      this.axios
            .post(
              "api/sys/auth/sysuser/update?email=" +
                this.editsysUserMessage.email +
                "&name=" +
                this.editsysUserMessage.name +
                "&phone=" +
                this.editsysUserMessage.phone +
                "&address=" +
                this.editsysUserMessage.address +
                "&sysUserId=" +
                this.editsysUserMessage.sysUserId
            )
            .then((response) => {
              console.log("response", response);
              if (response.data.code == 200) {
                this.$message({
                  message: "修改成功",
                  type: "success",
                });
                this.getList(1);
                this.editsysUserDialogVisible = false;
              } else {
                this.$message.error(response.data.message);
              }
            })
            .catch((error) => {
              this.$message.error("修改失败");
            });
    },
    // getProvinceList() {
    //   this.axios
    //     .get("/api/sys/auth/listProvince")
    //     .then((response) => {
    //       if (response.data.code == "200") {
    //         this.ProvinceList = response.data.value;
    //       } else {
    //         this.$message.error(response.data.message);
    //       }
    //     })
    //     .catch((error) => {
    //       this.$message.error("无法获取数据");
    //     });
    // },
    uploadface() {},
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    handleAvatarSuccess(res) {
      console.log("res", res);
      if (res.code != "200") {
        this.$message.error("文件格式不支持");
      } else {
        this.$message({
          message: "上传成功",
          type: "success",
        });
        this.addsysUserMessage.faceUrl = res.value;
        console.log(this.addsysUserMessage.faceUrl);
      }
    },
    beforeAvatarUpload(file) {
      console.log("file.type", file.type);
      const isimg = file.type === "image/png" || "image/jpeg";
      const isLt10M = file.size / 1024 / 1024 < 10;

      if (!isimg) {
        this.$message.error("上传头像图片只能是 JPG 或 PNG 格式!");
      }
      if (!isLt10M) {
        this.$message.error("上传头像图片大小不能超过 10MB!");
      }
      return isimg && isLt10M;
    },
    addsysUser() {
      console.log("birth", this.addsysUserMessage.birth);
      this.loading = true;
      this.$refs.addsysUserMessage.validate((valid) => {
        if (valid) {
          this.axios
            .post(
              "api/sys/auth/sysuser/add?email=" +
                this.addsysUserMessage.email +
                "&password=" +
                this.addsysUserMessage.password +
                "&name=" +
                this.addsysUserMessage.name +
                "&phone=" +
                this.addsysUserMessage.phone +
                "&address=" +
                this.addsysUserMessage.address +
                "&birth=" +
                this.addsysUserMessage.birth +
                "&faceUrl=" +
                this.addsysUserMessage.faceUrl +
                "&sex=" +
                this.addsysUserMessage.sex
            )
            .then((response) => {
              console.log("response", response);
              if (response.data.code == 200) {
                this.$message({
                  message: "添加成功",
                  type: "success",
                });
                this.getList(1);
                this.addsysUserDialogVisible = false;
              } else {
                this.$message.error(response.data.message);
              }
            })
            .catch((error) => {
              this.$message.error("添加失败");
            });
        } else {
          this.$message.error("请填写全部内容");
          return false;
        }
      });
      this.loading = false;
    },
    handleResetSearch() {
      this.searchsysUserList = {
        name: "",
        status: "",
        pageNum: "1",
        pageSize: "5",
      };
    },
    handleSearchList() {
      this.listLoading = true;
      this.axios
        .get("api/sys/auth/sysuser/list", {
          params: this.searchsysUserList,
        })
        .then((response) => {
          if (response.data.code == "200") {
            this.sysUserList = response.data.value.data;
            if (
              response.data.value.total / 5 >
              Number.parseInt(response.data.value.total / 5)
            ) {
              this.pageCunt =
                Number.parseInt(response.data.value.total / 5) + 1;
            } else {
              this.pageCunt = Number.parseInt(response.data.value.total / 5);
            }
            this.currentPage = 1;
          } else {
            this.$message.error(response.data.message);
          }
          this.listLoading = false;
        })
        .catch((error) => {
          this.$message.error("无法获取数据");
          this.listLoading = false;
        });
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    handleBan(banEmail) {
      this.$confirm("请再次确认是否禁用该员工")
        .then((_) => {
          this.axios
            .post(
              "api/sys/auth/sysuser/update?email=" +
                banEmail +
                "&status=" +
                "BAN"
            )
            .then((response) => {
              if (response.data.code == "200") {
                this.$message({
                  message: "禁用成功",
                  type: "success",
                });
                this.getList(1);
              } else {
                this.$message.error(response.data.message);
              }
            })
            .catch((error) => {
              this.$message.error("禁用失败");
            });
        })
        .catch((_) => {});
    },
    handleCancleBan(banEmail) {
      this.$confirm("请再次确认是否解除禁用该员工")
        .then((_) => {
          this.axios
            .post(
              "api/sys/auth/sysuser/update?email=" +
                banEmail +
                "&status=" +
                "NORMAL"
            )
            .then((response) => {
              if (response.data.code == "200") {
                this.$message({
                  message: "解除禁用成功",
                  type: "success",
                });
                this.getList(1);
              } else {
                this.$message.error(response.data.message);
              }
            })
            .catch((error) => {
              this.$message.error("解除禁用失败");
            });
        })
        .catch((_) => {});
    },
    handleDelete(deleteEmail) {
      this.$confirm("请再次确认是否删除该员工")
        .then((_) => {
          this.axios
            .post(
              "api/sys/auth/sysuser/update?email=" +
                deleteEmail +
                "&status=" +
                "DELETE"
            )
            .then((response) => {
              if (response.data.code == "200") {
                this.$message({
                  message: "删除成功",
                  type: "success",
                });
                this.getList(1);
              } else {
                this.$message.error(response.data.message);
              }
            })
            .catch((error) => {
              this.$message.error("删除失败");
            });
        })
        .catch((_) => {});
    },
    formatDateC(row) {
      // 获取单元格数据
      let datac = row.ctime;
      let dtc = new Date(datac);
      //获取月,默认月份从0开始
      let dtuMonth = dtc.getMonth() + 1;
      //获取日
      let dtuDay = dtc.getDate();
      //处理1-9月前面加0
      if (dtuMonth < 10) {
        dtuMonth = "0" + (dtc.getMonth() + 1);
      }
      //处理1-9天前面加0
      if (dtuDay < 10) {
        dtuDay = "0" + dtc.getDate();
      }
      //获取小时
      let dtuHours = dtc.getHours();
      //处理1-9时前面加0
      if (dtuHours < 10) {
        dtuHours = "0" + dtc.getHours();
      }
      //获取分钟
      let dtuMinutes = dtc.getMinutes();
      //处理1-9分前面加0
      if (dtuMinutes < 10) {
        dtuMinutes = "0" + dtc.getMinutes();
      }
      //获取秒
      let dtuSeconds = dtc.getSeconds();
      //处理1-9秒前面加0
      if (dtuSeconds < 10) {
        dtuSeconds = "0" + dtc.getSeconds();
      }
      //组装年月日时分秒,按自己的要求来
      return dtc.getFullYear() + "/" + dtuMonth + "/" + dtuDay;
      //+ " " + dtuHours + ":" + dtuMinutes + ":" + dtuSeconds
    },
  },
};
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>